package com.fortune.common.spring.binder;

import java.beans.PropertyEditorSupport;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.fortune.constants.Constants;

public class UsualDateBinder extends PropertyEditorSupport{
	private Logger logger = LoggerFactory.getLogger(UsualDateBinder.class);

	@Override
	public String getAsText() {
		SimpleDateFormat sdf = new SimpleDateFormat(Constants.DATE_FORMAT_DAY);
		Date date = (Date) getValue();
		return sdf.format(date);
	}

	@Override
	public void setAsText(String text) throws IllegalArgumentException {
		SimpleDateFormat sdf = new SimpleDateFormat(Constants.DATE_FORMAT_DAY);
		Date date;
		try {
			date = sdf.parse(text);
			setValue(date);
		} catch (ParseException e) {
			setValue(null);
			logger.error("将字符串"+text+"以"+Constants.DATE_FORMAT_DAY+"格式反序列化失败", e);
		}
	}
	
}
